*** This do file generates the output for tables A14, A15, and A16 for specifications with no time-varying covariates
* If needed change global path to point to directory where files are stored on your computer
*global path "I:/DataSets5/Duncan/Dropbox/Recent Minimum Wage Changes/2020.12 NBER Update/JOLE Precommittment Replication"
global bootdir "$path/Estimates/Bootstrap/no-tvc"
global logdir "$path/Logfiles"


log using "$logdir/generate-elasticities-and-ci-no-tvc.log", replace



**** ESTIMATED COEFFICIENTS

* Set tempfiles for appending
tempfile ceplowskillnoswitchers20192019
tempfile ceplowskillnoswitchers20152019


tempfile ceplowskillswitchers20192019
tempfile ceplowskillswitchers20152015
tempfile ceplowskillswitchers20152019

tempfile newlowskillswitchers20192019

tempfile cepyoungnoswitchers20192019
tempfile cepyoungnoswitchers20152019

tempfile cepyoungswitchers20192019
tempfile cepyoungswitchers20152015
tempfile cepyoungswitchers20152019

tempfile newyoungswitchers20192019

tempfile overall

tempfile lowskillbaselinemeans
tempfile youngbaselinemeans

use "$bootdir/coef-emp-ow-mw-cep-lowskill-young-baseline-means.dta", clear

keep *_lowskill
rename *_lowskill *
save `lowskillbaselinemeans', replace

use "$bootdir/coef-emp-ow-mw-cep-lowskill-young-baseline-means.dta", clear

keep *_young
rename *_young *
save `youngbaselinemeans', replace 

* CEP Lowskill Switchers 2015-2015 Post Period
use "$bootdir/coef-emp-cep-lowskill-switchers-2015-2015.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-cep-lowskill-switchers-2015-2015.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `lowskillbaselinemeans', nogen

save `ceplowskillswitchers20152015'



* CEP Lowskill No Switchers 2019 Post Period
use "$bootdir/coef-emp-cep-lowskill-noswitchers-2019-2019.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-cep-lowskill-noswitchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `lowskillbaselinemeans', nogen

save `ceplowskillnoswitchers20192019'

* CEP Lowskill No Switchers 2015-2019 Post Period
use "$bootdir/coef-emp-cep-lowskill-noswitchers-2015-2019.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-cep-lowskill-noswitchers-2015-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `lowskillbaselinemeans', nogen

save `ceplowskillnoswitchers20152019'


* CEP Lowskill Switchers 2019 Post Period
use "$bootdir/coef-emp-cep-lowskill-switchers-2019-2019.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-cep-lowskill-switchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `lowskillbaselinemeans', nogen

save `ceplowskillswitchers20192019'

* CEP Lowskill Switchers 2015-2019 Post Period
use "$bootdir/coef-emp-cep-lowskill-switchers-2015-2019.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-cep-lowskill-switchers-2015-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `lowskillbaselinemeans', nogen

save `ceplowskillswitchers20152019'

* New Lowskill Switchers 2019 Post Period
use "$bootdir/coef-emp-new-lowskill-switchers-2019-2019.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-new-lowskill-switchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `lowskillbaselinemeans', nogen

save `newlowskillswitchers20192019'


* CEP Lowskill No Switchers 2019 Post Period
use "$bootdir/coef-emp-cep-young-noswitchers-2019-2019.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-cep-young-noswitchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `youngbaselinemeans', nogen


save `cepyoungnoswitchers20192019'

* CEP Young No Switchers 2015-2019 Post Period
use "$bootdir/coef-emp-cep-young-noswitchers-2015-2019.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-cep-young-noswitchers-2015-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `youngbaselinemeans', nogen

save `cepyoungnoswitchers20152019'


* CEP Young Switchers 2019 Post Period
use "$bootdir/coef-emp-cep-young-switchers-2019-2019.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-cep-young-switchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `youngbaselinemeans', nogen

save `cepyoungswitchers20192019'

* CEP Young Switchers 2015-2019 Post Period
use "$bootdir/coef-emp-cep-young-switchers-2015-2019.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-cep-young-switchers-2015-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `youngbaselinemeans', nogen

save `cepyoungswitchers20152019'


* CEP Young Switchers 2015-2015 Post Period
use "$bootdir/coef-emp-cep-young-switchers-2015-2015.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-cep-young-switchers-2015-2015.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `youngbaselinemeans', nogen

save `cepyoungswitchers20152015'

* New young Switchers 2019 Post Period
use "$bootdir/coef-emp-new-young-switchers-2019-2019.dta", clear
merge 1:1 _n using "$bootdir/coef-ow-mw-new-young-switchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 _n using `youngbaselinemeans', nogen


save `newyoungswitchers20192019'

* Append datsets together 
use `ceplowskillswitchers20152019', replace
append using `ceplowskillswitchers20152015'
append using `ceplowskillswitchers20192019'

append using `ceplowskillnoswitchers20152019'
append using `ceplowskillnoswitchers20192019'

append using `newlowskillswitchers20192019'

append using `cepyoungswitchers20152019'
append using `cepyoungswitchers20152015'
append using `cepyoungswitchers20192019'

append using `cepyoungnoswitchers20152019'
append using `cepyoungnoswitchers20192019'

append using `newyoungswitchers20192019'

* Generate and append overall low-skill and young averages

keep if stat == "beta"

preserve 

* NOTE: We do NOT include the post period 2015 only results in the "overall averages" calculations

drop if postmax == 2015

collapse emp_beta_all_allchange-emp_beta_cps_index ow_beta_allchange-mw_pre_index, by(sample)
gen stat = "beta"

save `overall', replace
restore

append using `overall'

replace policycat = "overall average" if policycat == ""


* Generate own wage and minimum wage elasticity estimates for each iteration

gen owe_all = (emp_beta_all_allchange/emp_pre_allchange)/(ow_beta_allchange/ow_pre_allchange)
gen owe_large = (emp_beta_all_large/emp_pre_large)/(ow_beta_large/ow_pre_large)
gen owe_small = (emp_beta_all_small/emp_pre_small)/(ow_beta_small/ow_pre_small)
gen owe_index = (emp_beta_all_index/emp_pre_index)/(ow_beta_index/ow_pre_index)

gen mwe_all = (emp_beta_all_allchange/emp_pre_allchange)/(mw_beta_allchange/mw_pre_allchange)
gen mwe_large = (emp_beta_all_large/emp_pre_large)/(mw_beta_large/mw_pre_large)
gen mwe_small = (emp_beta_all_small/emp_pre_small)/(mw_beta_small/mw_pre_small)
gen mwe_index = (emp_beta_all_index/emp_pre_index)/(mw_beta_index/mw_pre_index)

order policycat sample postmin postmax switchers, last


tab owe_small


keep if stat == "beta"




* Export coefficients for employment effect estimates
export excel emp_beta_all_allchange-emp_beta_cps_index policycat-switchers ///
using "$bootdir/Tables/Table-A14-coef-no-tvc.xlsx", firstrow(variables) nolabel keepcellfmt replace

* Export coefficients for wage effect estimates
export excel ow_beta_allchange-mw_beta_index policycat-switchers ///
using "$bootdir/Tables/Table-A15-coef-no-tvc.xlsx", firstrow(variables) nolabel keepcellfmt replace

* Export coefficients for elasticity estimates
export excel owe_all-mwe_index policycat-switchers ///
using "$bootdir/Tables/Table-A16-coef-no-tvc.xlsx", firstrow(variables) nolabel keepcellfmt replace
*/




**** BOOTSTRAPPED CONFIDENCE INTERVALS

* Set tempfiles for appending
tempfile ceplowskillnoswitchers20192019
tempfile ceplowskillnoswitchers20152019

tempfile ceplowskillswitchers20152015
tempfile ceplowskillswitchers20152019
tempfile ceplowskillswitchers20192019

tempfile newlowskillswitchers20192019

tempfile cepyoungnoswitchers20192019
tempfile cepyoungnoswitchers20152019

tempfile cepyoungswitchers20152015
tempfile cepyoungswitchers20152019
tempfile cepyoungswitchers20192019


tempfile newyoungswitchers20192019

tempfile loci
tempfile hici
tempfile loci_overall
tempfile hici_overall

tempfile ci
tempfile ci_overall

* Set tempfiles for CS 2017 analysis
tempfile ceplowskillswitchers2019cs2017
tempfile ceplowskillswitchers2015cs2017

tempfile cepyoungswitchers2019cs2017
tempfile cepyoungswitchers2015cs2017

tempfile lowskillbaselinemeans
tempfile youngbaselinemeans

use "$bootdir/boot-emp-ow-mw-cep-lowskill-young-baseline-means.dta", clear

keep iteration *_lowskill
rename *_lowskill *
save `lowskillbaselinemeans', replace

use "$bootdir/boot-emp-ow-mw-cep-lowskill-young-baseline-means.dta", clear

keep iteration *_young
rename *_young *
save `youngbaselinemeans', replace 

* CEP Lowskill No Switchers 2019 Post Period
use "$bootdir/boot-emp-cep-lowskill-noswitchers-2019-2019.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-cep-lowskill-noswitchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `lowskillbaselinemeans', nogen

save `ceplowskillnoswitchers20192019'

* CEP Lowskill No Switchers 2015-2019 Post Period
use "$bootdir/boot-emp-cep-lowskill-noswitchers-2015-2019.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-cep-lowskill-noswitchers-2015-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `lowskillbaselinemeans', nogen

save `ceplowskillnoswitchers20152019'

* CEP Lowskill Switchers 2015 Post Period
use "$bootdir/boot-emp-cep-lowskill-switchers-2015-2015.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-cep-lowskill-switchers-2015-2015.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `lowskillbaselinemeans', nogen

save `ceplowskillswitchers20152015'
*/
* CEP Lowskill Switchers 2019 Post Period
use "$bootdir/boot-emp-cep-lowskill-switchers-2019-2019.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-cep-lowskill-switchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `lowskillbaselinemeans', nogen

save `ceplowskillswitchers20192019'

* CEP Lowskill Switchers 2015-2019 Post Period
use "$bootdir/boot-emp-cep-lowskill-switchers-2015-2019.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-cep-lowskill-switchers-2015-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `lowskillbaselinemeans', nogen

save `ceplowskillswitchers20152019'

* New Lowskill Switchers 2019 Post Period
use "$bootdir/boot-emp-new-lowskill-switchers-2019-2019.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-new-lowskill-switchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `lowskillbaselinemeans', nogen

save `newlowskillswitchers20192019'


* CEP Young No Switchers 2019 Post Period
use "$bootdir/boot-emp-cep-young-noswitchers-2019-2019.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-cep-young-noswitchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `youngbaselinemeans', nogen

save `cepyoungnoswitchers20192019'

* CEP Young No Switchers 2015-2019 Post Period
use "$bootdir/boot-emp-cep-young-noswitchers-2015-2019.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-cep-young-noswitchers-2015-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `youngbaselinemeans', nogen

save `cepyoungnoswitchers20152019'


* CEP Young Switchers 2015 Post Period
use "$bootdir/boot-emp-cep-young-switchers-2015-2015.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-cep-young-switchers-2015-2015.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `youngbaselinemeans', nogen

save `cepyoungswitchers20152015'
*/
* CEP Young Switchers 2019 Post Period
use "$bootdir/boot-emp-cep-young-switchers-2019-2019.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-cep-young-switchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `youngbaselinemeans', nogen

save `cepyoungswitchers20192019'

* CEP Young Switchers 2015-2019 Post Period
use "$bootdir/boot-emp-cep-young-switchers-2015-2019.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-cep-young-switchers-2015-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `youngbaselinemeans', nogen

save `cepyoungswitchers20152019'

* New young Switchers 2019 Post Period
use "$bootdir/boot-emp-new-young-switchers-2019-2019.dta", clear
merge 1:1 iteration using "$bootdir/boot-ow-mw-new-young-switchers-2019-2019.dta", keepusing(ow_beta_allchange-mw_beta_index) nogen
merge 1:1 iteration using `youngbaselinemeans', nogen

save `newyoungswitchers20192019'

* Append datsets together 
use `ceplowskillswitchers20152019', replace
append using `ceplowskillswitchers20152015'
append using `ceplowskillswitchers20192019'

append using `ceplowskillnoswitchers20152019'
append using `ceplowskillnoswitchers20192019'

append using `newlowskillswitchers20192019'

append using `cepyoungswitchers20152019'
append using `cepyoungswitchers20152015'
append using `cepyoungswitchers20192019'

append using `cepyoungnoswitchers20152019'
append using `cepyoungnoswitchers20192019'

append using `newyoungswitchers20192019'

* Fix emp_beta_all_small
egen emp_beta_all_small_temp = rowmean(emp_beta_acs_small emp_beta_cps_small)
replace emp_beta_all_small = emp_beta_all_small_temp



* Generate own wage and minimum wage elasticity estimates for each iteration

gen owe_all = (emp_beta_all_allchange/emp_pre_allchange)/(ow_beta_allchange/ow_pre_allchange)
gen owe_large = (emp_beta_all_large/emp_pre_large)/(ow_beta_large/ow_pre_large)
gen owe_small = (emp_beta_all_small/emp_pre_small)/(ow_beta_small/ow_pre_small)
gen owe_index = (emp_beta_all_index/emp_pre_index)/(ow_beta_index/ow_pre_index)


gen mwe_all = (emp_beta_all_allchange/emp_pre_allchange)/(mw_beta_allchange/mw_pre_allchange)
gen mwe_large = (emp_beta_all_large/emp_pre_large)/(mw_beta_large/mw_pre_large)
gen mwe_small = (emp_beta_all_small/emp_pre_small)/(mw_beta_small/mw_pre_small)
gen mwe_index = (emp_beta_all_index/emp_pre_index)/(mw_beta_index/mw_pre_index)

order policycat sample postmin postmax switchers iteration, last


tab owe_small

*emp_beta_all_allchange ow_pre_allchange mw_pre_index owe_small mwe_index
preserve
* Collapse to generate upper and lower cis for each outcome from the bootstrapped distribution
gcollapse (p2.5) emp_beta_all_allchange-mwe_index, by(policycat sample postmin postmax switchers)

foreach var of varlist emp_beta_all_allchange-mwe_index {
	rename `var' `var'_loci

}
save `loci'
restore 


preserve
* Collapse to generate upper and lower cis for each outcome from the bootstrapped distribution
gcollapse (p97.5) emp_beta_all_allchange-mwe_index, by(policycat sample postmin postmax switchers)

foreach var of varlist emp_beta_all_allchange-mwe_index {
	rename `var' `var'_hici

}
save `hici'
restore 

preserve

* NOTE: We do NOT include the post period 2015 only results in the "overall averages" calculations

drop if postmax == 2015

* Collapse to generate upper and lower cis for each outcome from the bootstrapped distribution across all groups
gcollapse (p2.5) emp_beta_all_allchange-mwe_index, by(sample)
foreach var of varlist emp_beta_all_allchange-mwe_index {
	rename `var' `var'_loci

}
save `loci_overall'
restore 


preserve

* NOTE: We do NOT include the post period 2015 only results in the "overall averages" calculations

drop if postmax == 2015

* Collapse to generate upper and lower cis for each outcome from the bootstrapped distribution across all groups
gcollapse (p97.5) emp_beta_all_allchange-mwe_index, by(sample)
foreach var of varlist emp_beta_all_allchange-mwe_index {
	rename `var' `var'_hici

}
save `hici_overall'
restore

preserve
* Merge Lower and Upper CIs together
use `loci', clear
merge 1:1 policycat sample postmin postmax switchers using `hici', nogen
save `ci', replace
restore

cap drop _merge

preserve
* Merge Lower and Upper CIs together. Overall Averages
use `loci_overall', clear
merge 1:1 sample using `hici_overall', nogen
save `ci_overall', replace
restore 

cap drop _merge

use `ci', clear
append using `ci_overall'

replace policycat = "overall average" if policycat == ""

gsort sample policycat -switchers postmin

order emp_beta_all_allchange_* emp_beta_all_large_* emp_beta_all_small_* emp_beta_all_index_* emp_beta_acs_allchange_* emp_beta_acs_large_* emp_beta_acs_small_* emp_beta_acs_index_* emp_beta_cps_allchange_* emp_beta_cps_large_* emp_beta_cps_small_* emp_beta_cps_index_* ow_beta_allchange_* ow_beta_large_* ow_beta_small_* ow_beta_index_* mw_beta_allchange_* mw_beta_large_* mw_beta_small_* mw_beta_index_* emp_pre_allchange_* emp_pre_large_* emp_pre_small_* emp_pre_index_* ow_pre_allchange_* ow_pre_large_* ow_pre_small_* ow_pre_index_* mw_pre_allchange_* mw_pre_large_* mw_pre_small_* mw_pre_index_* owe_all_* owe_large_* owe_large_* owe_small_* owe_index_* mwe_all_* mwe_large_* mwe_small_* mwe_index_*, last




tostring emp_beta_all_allchange_loci-emp_beta_cps_index_hici, replace format(%15.3f) force


foreach var of varlist emp_beta_all_allchange_loci-emp_beta_cps_index_hici {
	replace `var' = usubinstr(`var', "0", "", 1) 
}

tostring ow_beta_allchange_loci-mwe_index_hici, replace format(%15.2f) force

* Generate confidence interval with comma and brackets
foreach var in emp_beta_all_allchange emp_beta_all_large emp_beta_all_small emp_beta_all_index emp_beta_acs_allchange emp_beta_acs_large emp_beta_acs_small emp_beta_acs_index emp_beta_cps_allchange emp_beta_cps_large emp_beta_cps_small emp_beta_cps_index ow_beta_allchange ow_beta_large ow_beta_small ow_beta_index mw_beta_allchange mw_beta_large mw_beta_small mw_beta_index emp_pre_allchange emp_pre_large emp_pre_small emp_pre_index ow_pre_allchange ow_pre_large ow_pre_small ow_pre_index mw_pre_allchange mw_pre_large mw_pre_small mw_pre_index owe_all owe_large owe_small owe_index mwe_all mwe_large mwe_small mwe_index {
	gen `var'_ci = "[" + `var'_loci + "," + `var'_hici + "]"
}

order policycat-switchers, first 

drop emp_beta_all_allchange_loci-mwe_index_hici

* Export CIs for employment effect estimates
export excel policycat-switchers emp_beta_all_allchange_ci-emp_beta_cps_index_ci ///
using "$bootdir/Tables/Table-A14-ci-no-tvc.xlsx", firstrow(variables) nolabel keepcellfmt replace

* Export CIs for wage effect estimates
export excel policycat-switchers ow_beta_allchange_ci-mw_beta_index_ci ///
using "$bootdir/Tables/Table-A15-ci-no-tvc.xlsx", firstrow(variables) nolabel keepcellfmt replace


* Export CIs for elasticities
preserve
keep if policycat == "overall average"
export excel sample owe_all_ci-mwe_index_ci ///
using "$bootdir/Tables/Table-A16-ci-no-tvc.xlsx", firstrow(variables) nolabel keepcellfmt replace
restore
